package cc.loac.heba.controller;

import cc.loac.heba.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class CommentController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    CommentService commentService;

    @GetMapping("/comment")
    @ResponseBody
    public String getComment(@RequestParam("heiId") String heiId,
                            @RequestParam("email") String email,
                            HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        return commentService.getCommentByHeiId(heiId, email, token);
    }

    @PostMapping("/comment")
    @ResponseBody
    public String addComment(@RequestParam("heiId") String heiId,
                             @RequestParam("email") String email,
                             @RequestParam("content") String content,
                             @RequestParam(value = "parentId", required = false) String parentId,
                             @RequestParam(value = "replyId", required = false) String replyId,
                             HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": addComment: " + content);
        return commentService.addComment(heiId, email, content, parentId, replyId, token);
    }

    @DeleteMapping("/comment")
    @ResponseBody
    public String deleteComment(@RequestParam("commentId") String commentId,
                                @RequestParam("email") String email,
                                HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": deleteComment");
        return commentService.deleteComment(commentId, email, token);
    }

}
